var start;
var end;
var timelabels;
var temperatures;
var humiditys;
var winds;
var sum_hum;
var sum_temp;
var sum_wind;
var timeperiod;
$("a").bind("click",function(e){
	var tteemmpp=$(e.target).text();
	if(tteemmpp=="Sample"){
		timeperiod="WeatherInfo";
	}
	else if(tteemmpp=="Day"){
		timeperiod="WeatherInfoDay";
	}
	else if(tteemmpp=="Month"){
		timeperiod="WeatherInfoMonth";
	}
	$("#tp").text(tteemmpp);
});
function func002(){
	var ds=$("#datepicker1").val();
	start= new Date(Date.parse(ds.replace(/-/g,"/")));
	var ds=$("#datepicker2").val();
	end= new Date(Date.parse(ds.replace(/-/g,"/")));
}
function func001(){
	func002();
	alert(start.getTime());
			$.ajax({
				type : "GET",
				async : false,
				url : "/viewweather",
				data: "start="+start.getTime()+"&end="+end.getTime()+"&timeperiod="+timeperiod,
				success : function(data)
				{
					timelabels=data.timelabel;
					temperatures=data.temp;
					humiditys=data.humidity;
					winds=data.wind;
					sum_temp=data.sum_temp;
					sum_hum=data.sum_hum;
					sum_wind=data.sum_wind;
					drawcharts();
					filltable();
				},
				dataType : "json"
			}
			);
		}
$('.form_datetime').datetimepicker({
    //language:  'fr',
    weekStart: 1,
    todayBtn:  1,
	autoclose: 1,
	todayHighlight: 1,
	startView: 2,
	forceParse: 1,
    showMeridian: 1
});
function func1(){
	var ds=$("#datepicker1").val();
	var start= new Date(Date.parse(ds.replace(/-/g,"/")));
	var ds=$("#datepicker2").val();
	var end= new Date(Date.parse(ds.replace(/-/g,"/")));
	window.location="showchart.jsp?start="+start.getTime()+"&end="+end.getTime();
}
function max(array){
	var i=0;
	
	var maxvalue=array[0];
	for(i=0;i<=array.length-1;i++){
		if(array[i]>maxvalue){
			maxvalue=array[i];
		}
	}
	return maxvalue;
}
function min(array){
	var i=0;
	var minvalue=array[0];
	for(i=0;i<=array.length-1;i++){
		if(array[i]<minvalue){
			minvalue=array[i];
		}
	}
	return minvalue;
}
function filltable(){
	$("#averagetemp").text(sum_temp);
	$("#averagehum").text(sum_hum);
	$("#averagewind").text(sum_wind);
	
}
function drawcharts(){
	var datatemp = {
			labels : timelabels,
			datasets : [
				{
					fillColor : "rgba(151,187,205,0.5)",
					strokeColor : "rgba(151,187,205,1)",
					pointColor : "rgba(151,187,205,1)",
					pointStrokeColor : "#fff",
					data : temperatures
				}
			]
		};
	var step=max(temperatures)-min(temperatures)+1;
	var optiontemp={
			scaleOverride : true,
			scaleStartValue : min(temperatures),
			scaleStepWidth : 1,
			scaleSteps : step,
	};
	var datahum = {
			labels : timelabels,
			datasets : [
				{
					fillColor : "rgba(151,187,205,0.5)",
					strokeColor : "rgba(151,187,205,1)",
					pointColor : "rgba(151,187,205,1)",
					pointStrokeColor : "#fff",
					data : humiditys
				}
			]
		};
	step=max(humiditys)-min(humiditys)+1;
	var optionhum={
			scaleOverride : true,
			scaleStartValue : min(humiditys),
			scaleStepWidth : 1,
			scaleSteps : step,
	};
	var datawind = {
			labels : timelabels,
			datasets : [
				{
					fillColor : "rgba(151,187,205,0.5)",
					strokeColor : "rgba(151,187,205,1)",
					pointColor : "rgba(151,187,205,1)",
					pointStrokeColor : "#fff",
					data : winds
				}
			]
		};
	step=max(winds)-min(winds)+1;
	var optionwind={
			scaleOverride : true,
			scaleStartValue : min(winds),
			scaleStepWidth : 1,
			scaleSteps : step,
	};
	var tempLine = new Chart(document.getElementById("temperature").getContext("2d")).Line(datatemp,optiontemp);
	var tempLine = new Chart(document.getElementById("humidity").getContext("2d")).Line(datahum,optionhum);
	var tempLine = new Chart(document.getElementById("wind").getContext("2d")).Line(datawind,optionwind);
	
}



